package com.yy.bootdemo.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.bootdemo.admin.dao.MyGoodsDao;
import com.yy.bootdemo.admin.entity.BuyUser;
import com.yy.bootdemo.admin.entity.MyGoodsEntity;
import com.yy.bootdemo.admin.service.MyGoodsService;
import com.yy.bootdemo.redis.RedisManager;
import com.yy.bootdemo.utils.R;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;


/**
 * @author yy_pc
 */
@Service("myGoodsService")
public class MyGoodsServiceImpl extends ServiceImpl<MyGoodsDao, MyGoodsEntity> implements MyGoodsService {

    @Resource
    MyGoodsDao myGoodsDao;

    @Autowired
    private RedisManager redisTemplate;
    @Override
    public R cacheGoods(Long goodsId) {
        MyGoodsEntity myGoodsEntity=myGoodsDao.selectById(goodsId);
        redisTemplate.set(String.valueOf(myGoodsEntity.getGoodsId()),String.valueOf(myGoodsEntity.getStock()));
        return R.ok();
    }

    @Override
    public R buy(Long goodsId,Integer userId) {

        String script1 = "local v = redis.call('decrby', KEYS[1], tonumber(ARGV[1]));if v then if v< tonumber(ARGV[2]) then return 0;end;redis.call('set', KEYS[2], tonumber(ARGV[3]));end;return 0;";
        Long result1 = (Long)redisTemplate.execute(
                new DefaultRedisScript<>(script1, Long.class),
                Arrays.asList("1", "user:"+userId),
                new Object[]{"1", "0", ""+goodsId});
        System.out.println(result1);

        return R.ok(result1);
    }

    @Override
    public R lpush(Long goodsId) {
        MyGoodsEntity myGoodsEntity=myGoodsDao.selectById(goodsId);
if (myGoodsEntity!=null){
    Integer stock=  myGoodsEntity.getStock();
    String luaScript ="for i=1,"+stock+",1 do redis.call('lpush',KEYS[1], ARGV[1]);end; return 1;";
    //   String script1 = "local v = redis.call ('lpop', KEYS[1]);if v then  redis.call ('set', KEYS[2], ARGV[1]); return 1 else return 0; end;";
    Long result1 = (Long) redisTemplate.execute(
            new DefaultRedisScript<>(luaScript, Long.class),
            Arrays.asList("goodsId_x:" + myGoodsEntity.getGoodsId()),
            new Object[]{String.valueOf(myGoodsEntity.getGoodsId())});

    if (result1>0){  return R.ok("成功");}else {  return R.error("失败");}

}else {
    return R.ok("商品不存在");
}


    }

    @Override
    public R lpushbuy(BuyUser buyUser) {
        String script0 = "  local u = redis.call ('get', KEYS[1]);if u then   return 2;  else   local v = redis.call ('lpop', KEYS[2]);if v then  redis.call ('set', KEYS[1], ARGV[1]); return 1 else return 0; end;  end;  ";
        String script1 = "  local u = redis.call ('get', KEYS[1]);if u then   return 2;  else   local v = redis.call ('lpop', KEYS[2]);if v then  redis.call ('set', KEYS[1], ARGV[1], 'nx', 'ex', ARGV[2]); return 1 else return 0; end;  end;  ";
        Long result1 = (Long)redisTemplate.execute(
                new DefaultRedisScript<>(script1, Long.class),
                Arrays.asList("user_x:" + buyUser.getUserId()+"goodsId_x:" + buyUser.getGoodsId(),"goodsId_x:" + buyUser.getGoodsId()),
                new Object[]{String.valueOf(buyUser.getGoodsId()),"100"});
        System.out.println("result1!!!!!!!!!!!!!!!!!!!!!! ========="+result1);
        if (result1==0){
            System.out.println("结束"); return R.ok("结束");
        } else if (result1==1){
            System.out.println("成功"); return R.ok("成功");
        } else if (result1==2){
            System.out.println("已经获取到了不逼重复获取"); return R.ok("已经获取到了不逼重复获取");
        }else {
            System.out.println("失败");  return R.ok("失败");
        }

    }
}